package com.lastproject.ejb.util;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.lastproject.dto.WaitingListDTO;
import com.lastproject.ejb.entities.Book;
import com.lastproject.ejb.entities.User;
import com.lastproject.ejb.entities.WaitingList;

/**
 * Session Bean implementation class WaitingListConversion
 */
@Stateless
@LocalBean
public class WaitingListConversion {

	/**
	 * Default constructor.
	 */
	@PersistenceContext
	EntityManager em;

	public WaitingListConversion() {
		// TODO Auto-generated constructor stub
	}

	public WaitingListDTO fromEntity(WaitingList wl) {

		WaitingListDTO waitingDTO = new WaitingListDTO();
		waitingDTO.setId(wl.getId());
		waitingDTO.setDate(wl.getDate());
		waitingDTO.setEmergency(wl.isEmergency());
		waitingDTO.setIdUser(wl.getUser().getId());
		waitingDTO.setIdBook(wl.getBook().getId());
		return waitingDTO;

	}

	public WaitingList fromDTO(WaitingListDTO waitingListDTO) {

		WaitingList waitingList = em.find(WaitingList.class,
				waitingListDTO.getId());

		if (waitingList == null) {
			waitingList = new WaitingList();
		}

		waitingList.setUser(em.find(User.class, waitingListDTO.getIdUser()));
		waitingList.setBook(em.find(Book.class, waitingListDTO.getIdBook()));
		waitingList.setDate(waitingListDTO.getDate());
		waitingList.setEmergency(waitingListDTO.isEmergency());

		return waitingList;

	}

}